﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RMS.Utils.MVP;
using RMS.Web.Shell.View;
using RMS.DataAccess.Repositories;
using RMS.DataAccess.Entities;
using RMS.Utils.Authentication;
namespace RMS.Web.Shell.Presenter
{
    public class AutoCompleteSevicesPresenter : Presenter<IAutoCompleteSevicesView>
    {
        PatientsRepository _patientsRepository;
        IAuthentication _iAuthentication;
        public AutoCompleteSevicesPresenter(PatientsRepository patientsRepository, IAuthentication iAuthentication)
        {
            _patientsRepository = patientsRepository;
            _iAuthentication = iAuthentication;
        }

        public string[] GetPationts(string Name)
        {
            if (_iAuthentication.CurrentLogedInMember.UserName != null)
            {
                List<string> pationtName = new List<string>();
                string[] names = Name.Split(' ');

                var Pationts = _patientsRepository.Find(items =>
                {
                    items.FirstName = string.IsNullOrEmpty(names[0]) ? "%" : names[0] + "%";
                    if (names.Count()>1)
                    items.FamilyName = string.IsNullOrEmpty(names[1]) ? "%" : names[1] + "%";

                });

                foreach (Patients pationt in Pationts)
                {
                    pationtName.Add(pationt.FirstName +" "+ pationt.FamilyName);
                }
                return pationtName.ToArray();
            }
            else
                return null;
        }

    }
}
